package com.appspot.finajjarane.bo.mvc.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.finajjarane.framework.generic.ApplicationConstants;
import com.appspot.finajjarane.framework.models.ArticleModel;
import com.appspot.finajjarane.framework.service.IArticleService;
import com.appspot.finajjarane.framework.service.IImageService;


@Controller
@RequestMapping("/articles")
public class Articles {

	private static String URL_NEW = "/bo/articles/new";
	private static String URL_SAVE = "/bo/articles/save";
	private static String URL_SAVE_EDIT = "/bo/articles/save-edit";
	private static String URL_LIST = "/bo/articles/list/1";


	@Autowired
	IArticleService iArticleService;

	@Autowired
	IImageService iImageService;

	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ModelAndView save(
			@RequestParam("articleTitle")  			String   	 articleTitle,
			@RequestParam("articleBody")   			String   	 articleBody,
			@RequestParam("articleTags[]") 			List<String>  articleTags,
			@RequestParam("articleLang")   			String		 articleLang,
			HttpServletRequest httpServletRequest){




		ArticleModel articleModel = new ArticleModel();

		articleModel.setTitle(articleTitle.trim());
		articleModel.setBody(articleBody);
		articleModel.setTags(articleTags);
		articleModel.setLang(articleLang);
		articleModel.setImageSmall(iImageService.getImageKeyUploaded(httpServletRequest, "articleImageSmall"));
		articleModel.setImageCarousel(iImageService.getImageKeyUploaded(httpServletRequest, "articleImageCarousel"));

		Map<String, Object> data = new HashMap<String, Object>();

		try {
			iArticleService.articleCreateNew(articleModel);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("redirect:" + URL_NEW, data);

	}



	@RequestMapping(value="/new")
	public ModelAndView create(){

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("formUrl", iImageService.createUploadUrl(URL_SAVE));

		return new ModelAndView("articles/new",map);
	}

	@RequestMapping(value="/edit/{id}")
	public ModelAndView edit(@PathVariable int id){
		Map<String, Object> data = new HashMap<String, Object>();
		ArticleModel article;
		try {
			article = this.iArticleService.getArticle(new Long(id));
			data.put("article", article);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}

		data.put("formUrl", iImageService.createUploadUrl(URL_SAVE_EDIT));

		return new ModelAndView("articles/edit",data);
	}


	@RequestMapping(value="/save-edit", method=RequestMethod.POST)
	public ModelAndView save(
			@RequestParam("articleId")     										Long 		  articleId,
			@RequestParam("articleTitle")  										String   	  articleTitle,
			@RequestParam("articleBody")   										String   	  articleBody,
			@RequestParam("articleTags[]") 										List<String>  articleTags,
			@RequestParam("articleLang")   										String		  articleLang,
			@RequestParam(value="articleDeleteImageSmall", required = false)    boolean  	  articleDeleteImageSmall,
			@RequestParam(value="articleDeleteImageCarousel", required = false) boolean  	  articleDeleteImageCarousel,
			HttpServletRequest httpServletRequest){

		Map<String, Object> data = new HashMap<String, Object>();

		ArticleModel articleModel;
		try {
			articleModel = iArticleService.getArticle(articleId);

			articleModel.setTitle(articleTitle.trim());
			articleModel.setBody(articleBody);
			articleModel.setTags(articleTags);
			articleModel.setLang(articleLang);

			String keyImageSmall 	= iImageService.getImageKeyUploaded(httpServletRequest, "articleImageSmall");
			String keyImageCarousel = iImageService.getImageKeyUploaded(httpServletRequest, "articleImageCarousel");

			if(articleDeleteImageSmall){
				articleModel.setImageSmall(null);
			}
			else if(null!=keyImageSmall && !keyImageSmall.isEmpty()){
				articleModel.setImageSmall(keyImageSmall);
			}
			else{
				articleModel.setImageSmall(this.iArticleService.getArticle(articleModel.getId()).getImageSmall());
			}


			if(articleDeleteImageCarousel){
				articleModel.setImageCarousel(null);
			}
			else if(null!=keyImageCarousel && !keyImageCarousel.isEmpty()){
				articleModel.setImageCarousel(keyImageCarousel);
			}
			else{
				articleModel.setImageCarousel(this.iArticleService.getArticle(articleModel.getId()).getImageCarousel());
			}

			iArticleService.articleUpdate(articleModel);

		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("redirect:" + URL_LIST, data);
	}

	@RequestMapping(value="/list/{page}")
	public ModelAndView index(@PathVariable("page") int page){
		page=Math.max(page, 1);

		Map<String, Object> data = new HashMap<String, Object>();
		page = Math.max(page, 1);
		List<ArticleModel> articles;
		try {
			articles = iArticleService.getArticlesList(page, ApplicationConstants.NBR_ARTICLES_PER_PAGE_ADMIN, null);
			int articlesCount = iArticleService.getArticlesCount(null);
			double maxPages = Math.ceil((new Float(articlesCount).floatValue())/ApplicationConstants.NBR_ARTICLES_PER_PAGE_ADMIN);

			data.put("articles", articles);
			data.put("articlesCount", new Integer(articlesCount));
			data.put("maxPages", maxPages);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}


		return new ModelAndView("articles/list",data);
	}


	@RequestMapping("/remove/{articleId}")
	public ModelAndView remove(@PathVariable Long articleId){

		Map<String, Object> data = new HashMap<String, Object>();

		try {
			iArticleService.articleRemove(articleId);
		} catch (Exception e) {
			data.put(ApplicationConstants.VELOCITY_MESSAGE_HANDLER, e.getLocalizedMessage());
		}
		return new ModelAndView("redirect:" + URL_LIST, data);

	}


}
